package managers.conversationManager;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Properties;
import java.util.ResourceBundle;

import managers.OptionsManager;
import spotGUI.floatingChatGUI.FloatingChat;
import utils.StackTracePrinter;
import audio.AudioManager;

public class OnlineConversation extends Conversation implements Runnable {

	//private ConversationManager manager;
	public static final int WRITING = -1;
	public static final int STOP_WRITING = -2;
	public static final int TRILL = -3;

	private GenericSocket socket;

	private DataInputStream dis;
	private DataOutputStream dos;

	private Properties soundOptions = OptionsManager.getSharedInstance().getOptionGroup("soundPopup");

	public OnlineConversation(int userId, GenericSocket socket) throws IOException {

		this.userId = userId;
		this.socket = socket;
		chat = convManager.getChatManager().getChat(userId);
		
		dis = new DataInputStream(socket.getInputStream());
		dos = new DataOutputStream(socket.getOutputStream());

		chat.setEnabled(true);
		chat.enableFeatures("all", true);
		
		new Thread(this).start();
				
	}

	@Override
	public void run() {
		chat.updateStatus("", null);
		int lenght;
		byte[] message;

		try {
			while (true) {
				lenght = dis.readInt();
				switch (lenght) {
				case WRITING:
					chat.updateStatus(chat.getReceiverName()+ResourceBundle.getBundle("managers.conversationManager.messages").getString("m.3"), FloatingChat.WRITING_ICON);
					break;
				case STOP_WRITING:
					chat.updateStatus("", null);
					break;
				case TRILL:
					chat.updateConversation(ResourceBundle.getBundle("managers.conversationManager.messages").getString("m.4"), true, Calendar.getInstance().getTime());
					if (soundOptions.getProperty("trill.SOUND").equals("true"))
						try {
							String[] attributes = soundOptions.getProperty("trill.SOUNDPATH").split(",");
							AudioManager.getSharedInstance().play(attributes[0], attributes);
						} catch (Exception e) {
							//do nothing
							StackTracePrinter.print(e);
						}
					chat.setVisible(true);
					break;
				default:
					message = new byte[lenght];
					socket.getInputStream().read(message);
					chat.updateConversation(message, true, Calendar.getInstance().getTime());
					chat.setVisible(true);
				}
			}
		} catch (IOException e1) {
			
			chat.setConversation(new OfflineConversation(chat.getConversation().getUserId()));
		} finally {
			try {
				dis.close();
				dos.close();
				socket.close();
			} catch (IOException e) {
				//do nothing
			}
		}
	}

	public void closeConnection () throws IOException {
		socket.close();
	}


	@Override
	public void sendMessage (byte[] message) throws IOException {
		dos.writeInt(message.length);
		socket.getOutputStream().write(message);
	}

	@Override
	public void sendWritingState (int state) throws IOException {
		dos.writeInt(state);
	}
}
